/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sabayframework.activity;

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.TimeoutException;

import org.sabayframework.channel.Channel;

/**
 * Interface for activity monitoring.
 * 
 * @author Alex Antonau
 * */
public interface ActivityMonitor<I,O>{
	
	Activity<I, O> getActivity();
	Channel<I> getInput();
	Channel<O> getOutput();
	/**
	 * Returns number of milliseconds that activity spent in 'execute' method
	 * */	
	long getExecutionTime();
	/**
	 * Returns number of milliseconds between the moment of activity submission and 
	 * current moment or moment when activity was removed from the execution queue.
	 * */
	long getLiveTime();
	/**
	 * Returns number of calls to the Activity.execute method.
	 * */
	long getCallCount();
	/**
	 * Returns true if activity is still executing or is in execution queue.
	 * */
	boolean isAlive();
	/**
	 * Try to kill activity with given error.
	 * */
	void kill(Throwable error);
	/**
	 * Wait for a single result.
	 * @throws InvocationTargetException if activity fails
	 * @throws InterruptedException when wait was interrupted
	 * @throws TimeoutException when timeout has occurred. 
	 * @throws IllegalStateException if no data available after output 
	 * channel was closed.
	 * */
	O waitForResult(long timeoutMillis) throws 
										InvocationTargetException, 
										InterruptedException,
										TimeoutException, 
										IllegalStateException;
	/**
	 * Returns parent activity monitor if this activity was submitted by another
	 * activity.
	 * @return parent activity monitor or null if no parent activity exists.
	 * */
	ActivityMonitor<?,?> getParent();
		
}
